package com.example.hrmsaas.system.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.example.hrmsaas.company.controller.DepartmentController;
import com.example.hrmsaas.company.entity.Department;
import com.example.hrmsaas.company.vo.DeptVo;
import com.example.hrmsaas.system.entity.User;
import com.example.hrmsaas.system.service.IUserService;
import com.example.hrmsaas.system.utils.DataGridView;
import com.example.hrmsaas.system.utils.ResultObject;
import com.example.hrmsaas.system.utils.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 雷哥
 * @since 2021-11-04
 */
@RestController
@RequestMapping("/system/user")
public class UserController extends BaseController {

    private Log log = (Log) LogFactory.getLog(UserController.class);

    @Autowired
    IUserService userService;

    /***
     * 分配权限
     * @param map
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/setRole",method = RequestMethod.POST)
    public ResultObject setUserRole(@RequestBody Map<String,Object> map) throws ParseException
    {
        String userId = (String) map.get("id");
        List<String> roleIds = (List<String>) map.get("roleIds");
        if(userService.setRole(userId,roleIds)){
            return ResultObject.DISPATCH_SUCCESS;
        }else{
            return ResultObject.DISPATCH_ERROR;
        }

    }


    @RequestMapping("/userList")
    public DataGridView getUserList() throws ParseException {
        try {
            UserVo userVo = new UserVo();
            Integer page = request.getParameter("page") != null ? Integer.valueOf(request.getParameter("page")) : null;
            Integer limit = request.getParameter("limit") != null ? Integer.valueOf(request.getParameter("limit")) : null;
            UserVo UserVoQueryMapper = this.getUserVoQueryMapper(userVo, page, limit, CompanyId);
            log.info(UserVoQueryMapper.toString());
            return userService.getUserList(UserVoQueryMapper);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new DataGridView();
        }

    }

    private UserVo getUserVoQueryMapper(UserVo userVo, Integer page, Integer limit, String companyId) {
        if (null != page) userVo.setPage(page);
        if (null != limit) userVo.setLimit(limit);
        userVo.setCompanyId(companyId);
        return userVo;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultObject addUser(@RequestBody User user) throws ParseException {
        try {
            user.setCompanyId(CompanyId);
            user.setCompanyName(CompanyName);
            user.setName(user.getUsername());
            user.setSex(Integer.valueOf(user.getSex()));
            user.setFormOfEmployment(Integer.valueOf(user.getFormOfEmployment()));
            if (userService.addUser(user)) {
                return ResultObject.ADD_SUCCESS;
            } else {
                return ResultObject.ADD_ERROR;
            }
        } catch (Exception e) {
            log.error("新增用户异常" + e.getMessage());
            return ResultObject.ADD_ERROR;
        }

    }

    @RequestMapping("/info")
    public DataGridView userInfo() {
        if (!StringUtils.isEmpty(request.getParameter("id"))) {
            User data = userService.getUserByUid(request.getParameter("id"));
            return new DataGridView(data);
        } else {
            return new DataGridView();
        }
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultObject editUser(@RequestBody User user) {
        try {
            if (userService.updateUser(user)) {
                return ResultObject.SAVE_SUCCESS;
            } else {
                return ResultObject.SAVE_ERROR;
            }
        } catch (Exception e) {
            log.error("编辑会员出现异常：" + e.getMessage());
            return ResultObject.SAVE_ERROR;
        }
    }

    @RequestMapping("/del")
    public ResultObject delUser() throws ParseException {
        if (!StringUtils.isEmpty(request.getParameter("id"))) {
            if (userService.delUserById(request.getParameter("id"))) {
                return ResultObject.DELETE_SUCCESS;
            } else {
                return ResultObject.DELETE_SUCCESS;
            }
        } else {
            return new ResultObject(404, "缺少参数");
        }
    }


}

